﻿using DataLibrary;
using System;
using System.IO;
using System.Linq;

namespace GetDiskInfo
{
    class Program
    {
        private static ImageManagerEntities ime = new ImageManagerEntities();
        static void Main(string[] args)
        {
            CheckServerAndDisk();
            //Common.Email.SendEmail("wtlemon@126.com", "程序测试", "试试企业邮箱");
            //Console.ReadLine();
        }

        private static void CheckServerAndDisk()
        {
            Console.WriteLine("正在检查服务器和硬盘挂载情况...");
            Common.PublicMethod pm = new Common.PublicMethod();
            string IP = pm.GetLocalIP();
            string Port = "50001";
            int ServerInfoID = 0;
            int DiskInfoID = 0;
            var _serverInfo = ime.ServerInfo.Where(u => u.ServerIP == IP && u.ServerPort == Port);
            if (_serverInfo.Count() == 0)
            {
                ServerInfo serverInfo = new ServerInfo();
                serverInfo.ServerIP = IP;
                serverInfo.ServerPort = Port;
                serverInfo.Created_at = DateTime.Now;
                serverInfo.Updated_at = DateTime.Now;
                ime.ServerInfo.Add(serverInfo);
                ime.SaveChanges();
                ServerInfoID = serverInfo.ServerID;
                Console.WriteLine("添加了1台服务器,IP:" + IP + ",Port:" + Port, 2);
            }
            else
                ServerInfoID = _serverInfo.First().ServerID;
            DriveInfo[] allDrives = DriveInfo.GetDrives();
            foreach (DriveInfo item in allDrives)
            {
                if (item.IsReady)
                {
                    if (!string.IsNullOrEmpty(item.VolumeLabel) && (item.DriveType == DriveType.Fixed || item.DriveType == DriveType.Removable))//是否需要判定磁盘类型
                    {
                        int d = ime.DiskInfo.Where(u => u.DiskLabel == item.VolumeLabel).Count();//先检查数据库中是否存在当前硬盘；
                        DiskInfo diskinfo = new DiskInfo();
                        diskinfo.DiskName = item.Name;
                        diskinfo.DiskLabel = item.VolumeLabel;
                        diskinfo.DiskFree = item.TotalFreeSpace;
                        diskinfo.DiskSize = item.TotalSize;
                        diskinfo.Updated_at = DateTime.Now;
                        if (d == 0)
                        {
                            diskinfo.Created_at = DateTime.Now;
                            ime.DiskInfo.Add(diskinfo);
                            ime.SaveChanges();
                            Console.WriteLine("添加了1块硬盘,卷标:" + item.VolumeLabel);
                            DiskInfoID = diskinfo.DiskID;
                            var serverDisk = new ServerDisk();
                            serverDisk.DiskID = DiskInfoID;
                            serverDisk.ServerID = ServerInfoID;
                            serverDisk.Created_at = DateTime.Now;
                            serverDisk.Updated_at = DateTime.Now;
                            ime.ServerDisk.Add(serverDisk);
                            ime.SaveChanges();
                        }
                        else
                        {
                            ime.SaveChanges();
                            Console.WriteLine("硬盘:" + item.VolumeLabel + ",信息刷新完成");
                        }
                    }
                }
            }
            Console.WriteLine("硬盘挂载情况检查完成");
        }
    }
}
